1 /*
2 * Copyright (C) 2011 The Guava Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14
15 package com.google.common.cache;
16
17 /**
18 * Utility {@link Weigher} implementations intended for use in testing.
19 *
20 * @author Charles Fry
21 */
22 public class TestingWeighers {
23
24 /**
25 * Returns a {@link Weigher} that returns the given {@code constant} for every request.
26 */
27 static Weigher<Object, Object> constantWeigher(int constant) {
28 return new ConstantWeigher(constant);
29 }
30
31 /**
32 * Returns a {@link Weigher} that uses the integer key as the weight.
33 */
34 static Weigher<Integer, Object> intKeyWeigher() {
35 return new IntKeyWeigher();
36 }
37
38 /**
39 * Returns a {@link Weigher} that uses the integer value as the weight.
40 */
41 static Weigher<Object, Integer> intValueWeigher() {
42 return new IntValueWeigher();
43 }
44
45 static final class ConstantWeigher implements Weigher<Object, Object> {
46 private final int constant;
47
48 ConstantWeigher(int constant) {
49 this.constant = constant;
50 }
51
52 @Override
53 public int weigh(Object key, Object value) {
54 return constant;
55 }
56 }
57
58 static final class IntKeyWeigher implements Weigher<Integer, Object> {
59 @Override
60 public int weigh(Integer key, Object value) {
61 return key;
62 }
63 }
64
65 static final class IntValueWeigher implements Weigher<Object, Integer> {
66 @Override
67 public int weigh(Object key, Integer value) {
68 return value;
69 }
70 }
71
72 }